﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using AttributeRouting;
using AttributeRouting.Web.Mvc;
using Newtonsoft.Json.Linq;

using LuxDiner.Model.Results;
using LuxDiner.Model.Products;
using LuxDiner.Logic.Products;
namespace LuxDiner.Web.Management.Areas.Products.Controllers
{
    [RouteArea("Products")]
    [RoutePrefix("Product")]
    public class ProductApiController : ApiController
    {
        [Route("Gets")]
        [HttpPost]
        public JsonEntitySet<ProductModel> Gets(JObject request)
        {
            string errinfo = "no data";
            try
            {
                ProductQuery sea = new ProductQuery();
                sea.PageIndex = request.Value<int>("pageIndex");
                sea.PageSize = request.Value<int>("pageSize");
                sea.Key = request.Value<string>("key");
                sea.GetCategories = request.Value<bool>("getCategories");
                sea.Categories = request.Value<string>("categories");
                sea.Status = request.Value<int>("productStatus");
                var result = ProductLogic.GetProducts(sea);
                return new JsonEntitySet<ProductModel>(result);
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntitySet<ProductModel>(null, errinfo, false);
        }

        [Route("Save")]
        [HttpPost]
        [Authorize(Roles = "Admin,ProductManage")]
        public JsonEntity<ProductModel> Save(JObject request)
        {
            string errinfo = "对不起！保存失败，出错原因：";
            try
            {
                #region 收集参数
                ProductModel model = new ProductModel() { Categories = new ProductCategoryCollection() };
                model.ID = request.Value<int>("productID");
                model.ProductNo = request.Value<string>("productNo");
                model.ProductName = request.Value<string>("productName");
                model.UnitName = request.Value<string>("unitName");
                model.AssistantUnitName = request.Value<string>("assistantUnitName");
                if (!string.IsNullOrEmpty(request.Value<string>("assistantUnitRate")))
                    model.AssistantUnitRate = request.Value<int>("assistantUnitRate");
                var categories = request.Value<string>("categories");
                var catArray = categories.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (catArray.Length > 0)
                {
                    foreach (var c in catArray)
                    {
                        model.Categories.Add(new ProductCategoryModel() { ID = int.Parse(c) });
                    }
                }
                model.Status = request.Value<int>("status");
                model.SortIndex = request.Value<int>("sortIndex");
                #endregion

                if (model.ID == 0)
                    model = ProductLogic.CreateNew(model);
                else
                    ProductLogic.Update(model);
                return new JsonEntity<ProductModel>(model);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonEntity<ProductModel>(null, errinfo, false);
        }

        [Route("Delete")]
        [HttpPost]
        [Authorize(Roles = "Admin,ProductManage")]
        public JsonResult<bool> Delete(JObject request)
        {
            string errinfo = "对不起！删除失败，出错原因：";
            try
            {
                #region 收集参数
                var id = request.Value<int>("productID");
                #endregion
                ProductLogic.Delete(id);
                return new JsonResult<bool>(true);
            }
            catch (Utils.LuxDinerException lde)
            {
                errinfo += lde.Message;
            }
            catch (Exception ex)
            {
                errinfo = ex.Message;
                Common.Logger.Error("", ex);
            }
            return new JsonResult<bool>(false, errinfo, false);
        }
    }
}
